package com.sitanyun.merchant.guide.push;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;

import com.google.gson.Gson;
import com.sitanyun.merchant.guide.app.Contains;
import com.sitanyun.merchant.guide.bean.MessagesBean;
import com.sitanyun.merchant.guide.view.activity.SplashsActivity;

import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by jzl
 * on 2022/3/9.
 */
public class BoardcastReceiver extends BroadcastReceiver {
    private static final String TAG = "PushMessageReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();
        //通知ID
        if (Contains.ZHANDIANNAME.equals("前台")){
            Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.cancel();
            Log.d(TAG,Contains.ZHANDIANNAME);

        }
        Log.d(TAG, "onReceive - " + intent.getAction());
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);

            Log.d(TAG, "收到了自定义消息。消息内容是：" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            // 自定义消息不会展示在通知栏，完全要开发者写代码去处理
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            String string = bundle.getString(JPushInterface.EXTRA_EXTRA);
            Log.d(TAG, "收到了通知"+string);
            //通知ID
            int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
             if (Contains.ZHANDIANNAME.equals("前台")){
                 Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
                 vibrator.cancel();
                 Log.d(TAG,Contains.ZHANDIANNAME);
                 JPushInterface.clearNotificationById(context,notificationId);

             }
            // 在这里可以做些统计，或者做些其他工作
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "用户点击打开了通知");
            // 在这里可以自己写代码去定义用户点击后的行为
            try {
                /**
                 * 获取极光通知栏消息传递数据
                 */
                String string = bundle.getString(JPushInterface.EXTRA_EXTRA);
                if (string != null) {
                    String bizId=  new JSONObject(string).getString("bizId");
                    String clientTypeId=  new JSONObject(string).getString("clientTypeId");
                    String content=  new JSONObject(string).getString("content");
                    String remark=  new JSONObject(string).getString("remark");
                    MessagesBean testBean=  new Gson().fromJson(remark, MessagesBean.class);
//                    context.startActivity(new Intent(context, SplashsActivity.class).putExtra("cid",testBean)
////                             .putExtra("behavior","call")
//                              .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                    //
                  Log.e("LOGS",testBean.getBehavior()+"状态");
                   //回电
                  if (testBean.getBehavior().equals("CALL")){
                      Log.e("LOGS",testBean.getBehavior()+"CALL");
                      context.startActivity(new Intent(context, SplashsActivity.class).putExtra("cid",testBean)
                            //  .putExtra("behavior","call")
                              .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                  }  //同频
                  else if (testBean.getBehavior().equals("MEETING")){
                      Log.e("LOGS",testBean.getBehavior()+"MEETING");
                         //没有超过10秒
                          //跳转VR
                          context.startActivity(new Intent(context, SplashsActivity.class).putExtra("cid",testBean)
                         // .putExtra("behavior","meeting")
                          .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));


                  } //消息
                  else if (testBean.getBehavior().equals("MESSAGE")){
                      Log.e("LOGS",testBean.getBehavior()+"MESSAGE");
                      context.startActivity(new Intent(context, SplashsActivity.class).putExtra("cid",testBean)
                      //.putExtra("behavior","message")
                              .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                 }//浏览
                  else if (testBean.getBehavior().equals("BROWSE")){
                      Log.e("LOGS",testBean.getBehavior()+"BROWSE");
                      context.startActivity(new Intent(context, SplashsActivity.class).putExtra("cid",testBean)
                            //  .putExtra("behavior","browse")
                              .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

                  }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Log.d(TAG, "Unhandled intent - " + intent.getAction());
        }

    }
}